<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
      <span @click="changePosition(item)">{{item.name}}</span>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script>
  export default {
    created() {
      this.getBreadcrumb()
    },
    data() {
      return {
        levelList: null
      }
    },
    methods: {
      getBreadcrumb() {
        let matched = this.$route.matched.filter(item => item.name)
        const first = matched[0]
        if (first && (first.name !== '首页' || first.path !== '')) {
          matched = [{name: '首页', path: '/'}].concat(matched)
        }
        this.levelList = matched
      },
      changePosition(val) {
        if (val.name === '首页') {
          this.$router.push({
            path: val.path
          })
        }
      }
    },
    watch: {
      $route() {
        this.getBreadcrumb()
      }
    }
  }
</script>
<style scoped lang="scss">
  .el-breadcrumb {
    display: inline-block;
    font-size: 14px;
  }
</style>
